<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class WeightdataExport implements FromQuery, WithHeadings, WithMapping
{
    use ModelOperatorTrait;

	protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\Weighdata::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$weightdata = $this->model();

    	if(isset($this->conditions['system_number'])){
    		$weightdata = $weightdata->where('system_number','=',$this->conditions['system_number']);
    	}

    	if(isset($this->conditions['start_time'])){
    		$weightdata = $weightdata->where('system_datetime','>=',$this->conditions['start_time']);
    		$weightdata = $weightdata->where('system_datetime','<=',$this->conditions['end_time']);
    	}

    	$weightdata = $weightdata->orderBy('system_datetime','DESC');

        return $weightdata;
    }

    public function map($weightdata):array{
    	return [
    		$weightdata->system_number,
    		$weightdata->system_datetime,
    		$weightdata->process_number_desc,
    		$weightdata->trans_type_desc,
    		$weightdata->trans_status_desc,
    		$weightdata->vehicle_number,
    		$weightdata->order_systemnumber,
    		$weightdata->is_finished_desc,
    		$weightdata->site_code,
    		$weightdata->first_readic_datetime,
    		$weightdata->second_readic_datetime,
    		$weightdata->ic_id,
    		$weightdata->mater_number,
    		$weightdata->first_time,
    		$weightdata->first_weight,
    		$weightdata->first_wbid,
    		$weightdata->second_weight,
    		$weightdata->second_time,
    		$weightdata->second_wbid,
    		$weightdata->net_weight,
    		$weightdata->first_capvh_path.'/'.$weightdata->first_capvh_filename,
    		$weightdata->first_capvn_path.'/'.$weightdata->first_capvn_filename,
    		$weightdata->first_capvn,
    		$weightdata->second_capvh_path.'/'.$weightdata->second_capvh_filename,
    		$weightdata->second_capvn_path.'/'.$weightdata->second_capvn_filename,
    		$weightdata->second_capvn,
    		$weightdata->first_cap_a_path.'/'.$weightdata->first_cap_a_fielname,
    		$weightdata->first_cap_b_path.'/'.$weightdata->first_cap_b_fielname,
    		$weightdata->second_cap_a_path.'/'.$weightdata->second_cap_a_fielname,
    		$weightdata->second_cap_b_path.'/'.$weightdata->second_cap_b_fielname,
    		$weightdata->erp_bill_systemnumber,
    		$weightdata->erp_fxp_systemnumber,
    		$weightdata->remarks
    	];
    }

    public function headings():array{
    	return [
    		'业务单号',
    		'系统时间',
    		'业务流程类型',
    		'单据类型',
    		'单据状态',
    		'车牌号码',
    		'派车单或者送货通知书单号',
    		'是否完成',
    		'称重计量地点',
    		'初称刷卡时间',
    		'回称刷卡时间',
    		'IC卡号',
    		'物料编码',
    		'初称时间',
    		'初称重量',
    		'初称地磅编号',
    		'回称重量',
    		'回称时间',
    		'回称地磅编号',
    		'净重',
    		'初称抓拍车辆图片保存网址',
    		'初称识别车牌图片保存网址',
    		'初称识别车牌号码',
    		'回称抓拍车辆图片保存网址',
    		'回称识别车牌图片保存路',
    		'回称识别车牌号码',
    		'初称地磅A端抓拍图片保存网址',
    		'初称地磅B端抓拍图片保存网址',
    		'回称地磅A端抓拍图片保存网址',
    		'回称地磅B端抓拍图片保存网址',
    		'ERP的入库单或者发货单',
    		'辅销品发放单号',
    		'备注'
    	];
    }
}
